package com.loie.xcx.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.aop.SignatureValidation;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.model.bo.CustomerBO;
import com.loie.xcx.model.bo.PermissionBO;
import com.loie.xcx.model.dto.CustomerDTO;
import com.loie.xcx.service.CustomerService;
import com.loie.xcx.service.PermissionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhl
 */
@Slf4j
@RequestMapping("/customer")
@RestController
@AllArgsConstructor
public class CustomerController {

    private final CustomerService customerService;
    private final PermissionService permissionService;

    @SignatureValidation
    @PostMapping("/getCustomerInfo")
    public JsonResult<List<CustomerBO>> getCustomerInfo(@RequestBody String jsonStr) {
        CustomerDTO customerDTO = JSONObject.parseObject(jsonStr, CustomerDTO.class);
        String openId = customerDTO.getOpenId();
        Integer limit = customerDTO.getLimit();
        if (customerDTO.getCustomerCorpName() != null && StringUtils.isNotBlank(openId)) {
            if (limit == null || limit < 1) {
                customerDTO.setLimit(5);
            }
            System.out.println(customerDTO);
            String moduleName = "客户资料";
            List<PermissionBO> permissionBOList = permissionService.getPermission(openId, moduleName);
            List<CustomerBO> list;
            if (permissionBOList.size() != 0) {
                list = customerService.getCustomersByPermissions(permissionBOList, customerDTO);
            } else {
                list = new ArrayList<>();
            }
            return new JsonResult<>(list);
        }
        return new JsonResult<>(400, "查询失败");
    }
}
